import axios from 'axios'
import axiosFetch from '../../fetch/axios'

const state = {
    articleList: [],
    articleDetail: {}
}


const mutations = {
    setArticleList(state, data) {
        state.articleList = data
    },
    setArticleDetail(state, data) {
        state.articleDetail = data
    }
}

const actions = {
    getArticleList({ commit }, parmas) {
        return new Promise((resolve, reject) => {
            axios.get('/article/list', {　　
                params: { 'tid': 1 }
            }).then(function(response) {　
                commit('setArticleList', response.data);
                return resolve(response.data);
            }).catch(function(error) {　　
                console.log(error);　
                return reject(error)
            });

        })
    },
    getArticleDetail({ commit }, aid) {
        return new Promise((resolve, reject) => {
            axios.get('/article/articledetail', {　
                params: { 'aid': aid }
            }).then(function(response) {　
                commit('setArticleDetail', response.data);
                return resolve(response.data);
            }).catch(function(error) {　　
                console.log(error);
                return reject(error)　
            });
        })
    },
    updateArticleStar({ commit },params) {
        return new Promise((resolve, reject) => {
            axiosFetch.fetchPost('/article/updateStar', params).then(responseData => {
                return resolve(responseData);
            }, err => {
                return reject(err)
            });
        })
    }
}


const getters = {
    articleList: state => state.articleList,
    articleDetail: state => state.articleDetail
}

export default {
    state,
    mutations,
    actions,
    getters
}